package com.email.content;

import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import com.example.logindesign.R;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class EmailDetailActivity extends Activity {
	private static final String NAMESPACE = "http://tempuri.org/";
	private static final String ENDPOINT = "http://192.168.1.198:1536/CommunitySystemsicWsWF.asmx";

	//返回按钮
	private ImageButton btnBack;
	
	// textview控件 5个
	private TextView textViewTitle;
	private TextView textViewSender;
	private TextView textViewTime;
	private TextView textViewReceiver;
	private TextView textViewContent;
	
	// TextView的变量值
	private String id;
	private String textViewTitleValue = "";
	private String textViewSenderValue = "";
	private String textViewTimeValue = "";
	private String textViewReceiverValue = "";
	private String textViewContentValue = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_email_detail);
		// sharedpreference中取得相应值
		getValueFromSharedPreferences();
		// 初始化TextView
		initTextView();
	}
	
	/**
	 * sharedpreference中取得相应值
	 */
	private void getValueFromSharedPreferences() {
		// TODO Auto-generated method stub
		SharedPreferences sharedPreferences = getSharedPreferences("UserInfo",
				Activity.MODE_PRIVATE);
		id = sharedPreferences.getString("id", "");
	}
	
	/**
	 * 初始化TextView及button
	 */
	private void initTextView() {
		
		textViewTitle = (TextView) findViewById(R.id.textView_email_Title);
		textViewSender = (TextView) findViewById(R.id.textView_email_Sender);
		textViewTime = (TextView) findViewById(R.id.textView_email_Time);
		textViewReceiver = (TextView) findViewById(R.id.textView_email_Receiver);
		textViewContent = (TextView) findViewById(R.id.textView_email_Content);
		
		// 按钮
		btnBack = (ImageButton) findViewById(R.id.btn_notice_detail_back);
		// 返回按钮
		btnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 跳转到listview页面
				Intent intent = new Intent();
				intent.setClass(EmailDetailActivity.this,
						EmailListViewActivity.class);
				startActivity(intent);
			}
		});
		
		new setTextViewValueTask().execute();
	}
	
	/**
	 * 设置textView的值
	 */
	private class setTextViewValueTask extends AsyncTask<Void, Void, String> {

		protected void onPreExecute() {
			// 设置TextView的值
			setTextViewValue();
		}

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			// 事件详细参数需要 id,unitcode
			Intent getdetail = getIntent();
			id = getdetail.getStringExtra("id");

			String resultString = getData();
			Log.e("webservice取得", "完毕");
			String finallyResultString = jsonFormat(resultString);
			return finallyResultString;
		}

		protected void onPostExecute(String finallyResultString) {
			if (finallyResultString.equals("success")) {
				// 设置TextView的值
				setTextViewValue();
			}
		}

	}
	
	
	/**
	 * 得到详细信息
	 * 
	 * @return
	 */
	private String getData() {
		// TODO Auto-generated method stub
		String resultString = "";
		String methodName = "get_SysMailDetails";
		String soapAcion = "http://tempuri.org/get_SysMailDetails";
		SoapObject soapObject = new SoapObject(NAMESPACE, methodName);

		// 邮件id
		soapObject.addProperty("id", id);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(soapObject);
		envelope.dotNet = true;
		HttpTransportSE transportSE = new HttpTransportSE(ENDPOINT);
		try {
			transportSE.call(soapAcion, envelope);
			if (envelope.getResponse() != null) {
				SoapObject object = (SoapObject) envelope.bodyIn;
				resultString = object.getProperty(0).toString();
				Log.e("webservice取得", resultString);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resultString;
	}
	
	/**
	 * 解析json数据返回hashmap格式数据
	 * 
	 * @return
	 */
	private String jsonFormat(String resultString) {
		JSONArray jsonArray;
		try {
			jsonArray = new JSONArray(resultString);
			JSONObject jsonObject = jsonArray.optJSONObject(0);
			if (jsonObject.optString("Code").equals("1")) {
				// 根据实际从第2组开始取得有关数据
				jsonObject = jsonArray.optJSONObject(1);
				textViewSenderValue = jsonObject.optString("mailfrom");
				textViewContentValue = jsonObject.optString("mailcontent");
				textViewTimeValue = jsonObject.optString("mailtimereceive");
				textViewReceiverValue = jsonObject.optString("mailfrom");
				textViewTitleValue = jsonObject.optString("mailtitle");
				return "success";
			} else {
				// 没有数据，如何显示？已经到最后了
				// map.put("ItemTime", R.drawable.icon_settings);
				// map.put("ItemTitle", "xin这是第" + mItemCount + "行");
				// map.put("ItemText", "xin这是第" + mItemCount + "行");
				return "fail";
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "fail";
	}
	
	
	/**
	 * 设置TextView的值
	 */
	private void setTextViewValue() {
		// TODO Auto-generated method stub
		textViewReceiver.setText(textViewReceiverValue);
		textViewSender.setText(textViewSenderValue);
		textViewTime.setText(textViewTimeValue);
		textViewTitle.setText(textViewTitleValue);
		textViewContent.setText(textViewContentValue);
	}
	
	/**
	 * 跳转到listview页面
	 */
	private void skipListViewActivity() {
		Intent listviewIntent = new Intent();
		listviewIntent.setClass(EmailDetailActivity.this,
				EmailListViewActivity.class);
		EmailDetailActivity.this.startActivity(listviewIntent);
		EmailDetailActivity.this.finish();
	}
	
	/**
	 * 重写keydown事件
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			skipListViewActivity();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
